#include "MD2Viewer.h"

//
#include "Modules.h"

//
Hannover::Hannover()
:	_modules(NULL), _renderSystem(NULL), _sceneManager(NULL), _bufferManager(NULL),
	_fontManager(NULL), _freeTypeFont("Default Font")
{
	_windowTitle = "Tutorial Vertices";
}

Hannover::~Hannover()
{

}

bool Hannover::createModules()
{
	_modules = new Modules;
	
	//
	if (!_modules->create(_hwnd))
	{
		return false;
	}

	//
	_renderSystem = _modules->getRenderEngine()->getRenderSystem();

	//
	_sceneManager = _modules->getRenderEngine()->getSceneManager();
	//

	//
	_bufferManager = _modules->getRenderEngine()->getBufferFactory();

	//
	_fontManager = _modules->getRenderEngine()->getFontFactory();

	//
	return true;
}

bool Hannover::foreRender()
{
	//
	return true;
}

bool Hannover::rendering()
{
	//
	_renderSystem->beginFrame();

	//
	renderGeometry();

	//
	_renderSystem->endFrame();

	//
	return true;
}

bool Hannover::postRender()
{
	//
	return true;
}

bool Hannover::forePlay()
{
	//
	if (!createWindow())
	{
		return false;
	}

	//
	if (!adjustClientArea())
	{
		return false;
	}

	//
	if (!createModules())
	{
		return false;
	}

	//
	if (!createFonts())
	{
		return false;
	}

	//
	if (!setViewport())
	{
		return false;
	}
	
	//
	if (!initGeometry())
	{
		return false;
	}

	//
	DragAcceptFiles(_hwnd, true);

	//
	return true;
}

bool Hannover::postPlay()
{
	//
	if (_modules)
	{
		_modules->destroy();
	}

	//
	return true;
}

bool Hannover::update()
{
	//
	if (NULL == _modules)
	{
		return false;
	}

	//
	if (!_renderSystem->update())
	{
		return false;
	}

	//
	if (!Buddha::Mouse::getSingletonPtr()->update())
	{
		return false;
	}

	//
	if (Buddha::Mouse::getSingletonPtr()->isLeftButtonDown())
	{
		//_sceneManager->getSceneNode("Hello")->roll(0.001f);
		//_sceneManager->getSceneNode("Hello")->scale(0.99f, 0.99f, 0.99f);
	}


	//
	if (Buddha::Mouse::getSingletonPtr()->isKeyDown(VK_LEFT))
	{
		_sceneManager->getSceneNode("node")->yaw(0.001f);
	}
	//
	if (Buddha::Mouse::getSingletonPtr()->isKeyDown(VK_RIGHT))
	{
		_sceneManager->getSceneNode("node")->yaw(-0.001f);
	}
	//
	if (Buddha::Mouse::getSingletonPtr()->isKeyDown(VK_UP))
	{
		_sceneManager->getSceneNode("node")->roll(0.001f);
	}
	//
	if (Buddha::Mouse::getSingletonPtr()->isKeyDown(VK_DOWN))
	{
		_sceneManager->getSceneNode("node")->roll(-0.001f);
	}
	
	//
	return true;
}

bool Hannover::setViewport()
{
	//
	D3DVIEWPORT9 v;
	v.X = 0;
	v.Y = 0;
	v.Width = Buddha::WindowHelper::getSingletonPtr()->getClientWidth(_hwnd);
	v.Height = Buddha::WindowHelper::getSingletonPtr()->getClientHeight(_hwnd);
	v.MinZ = 0.0f;
	v.MaxZ = 1.0f;

	//
	if (!_modules->getRenderEngine()->getRenderSystem()->setViewport(&v))
	{
		return false;
	}

	//
	return true;
}

bool Hannover::initGeometry()
{
	//_model = _sceneManager->createEntityMD2("model", "../../bin/data/MD2/sydney/sydney.md2");
	_model = _sceneManager->createEntityMD2("model", "../../bin/data/MD3/rocket/rocket.md3");

	_node = _sceneManager->getRootSceneNode()->addSceneNodeChild("node");
	
	_node->attachObject(_model);
	
	//
	return true;
}

void Hannover::renderGeometry()
{
	_sceneManager->render();
}

bool Hannover::createFonts()
{
	if (NULL == _fontManager)
	{
		return false;
	}

	//
	std::string faceFile("c:\\WINDOWS\\Fonts\\times.ttf");
	
	//
	bool b = _fontManager->createFont(Euclid::FontType_FreeType, faceFile, 16, Euclid::FONT_PROP_NONE, _freeTypeFont);

	//
	return b;
}

void Hannover::onDragAndDrop()
{
	if (_model)
	{
		_model->destroy();
	}

	_model = _sceneManager->createEntityMD2("model", _dragDropFile);
	_node->reset();
	_renderSystem->resetCamera();
	_node->attachObject(_model);
}